package com.sky.controller.admin;

import com.sky.controller.BaseController;
import com.sky.dto.DishDTO;
import com.sky.entity.DishEntity;
import com.sky.enumeration.DishStatusEnum;
import com.sky.result.Result;
import com.sky.result.TableDataInfo;
import com.sky.service.DishService;
import com.sky.utils.TransformUtils;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/admin/dish")
@Slf4j
@Api(tags = "菜品模块")
public class DishController extends BaseController {

    @Autowired
    private DishService dishService;

    @PostMapping
    @ApiOperation("新增菜品")
    @CacheEvict(cacheNames = "dishCategoryIdCache", key = "#dishDTO.categoryId")
    public Result save(@RequestBody DishDTO dishDTO) {
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public TableDataInfo page(Integer page, Integer size, String status, Integer categoryId, String name) {
        DishStatusEnum statusEnum = TransformUtils.transformEnum(status, DishStatusEnum.class);
        List<DishVO> list = dishService.page(page, size, statusEnum, categoryId, name);
        return getTableInfo(list);
    }

    @GetMapping("/list")
    @ApiOperation("根据分类ID查询菜品")
    public TableDataInfo getByCategory(Integer categoryId) {
        List<DishVO> list = dishService.getByCategoryId(categoryId);
        return getTableInfo(list);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result detail(@PathVariable Integer id) {
        DishVO entity = dishService.getById(id);
        return Result.success(entity);
    }

    @PutMapping
    @ApiOperation("修改菜品")
    @CacheEvict(cacheNames = "dishCategoryIdCache", allEntries = true)
    public Result update(@RequestBody DishDTO dto) {
        dishService.update(dto);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售，停售")
    @CacheEvict(cacheNames = "dishCategoryIdCache", allEntries = true)
    public Result enableOrDisable(@PathVariable String status, Long id) {
        DishStatusEnum statusEnum = TransformUtils.transformEnum(status, DishStatusEnum.class);
        dishService.enableOrDisable(statusEnum, id);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("批量删除菜品")
    @CacheEvict(cacheNames = "dishCategoryIdCache", allEntries = true)
    // 1,2,3 可以通过 springmvc 自动转为 List，需添加 @RequestParam 注解
    public Result delBatch(@RequestParam List<Long> ids) {
        log.info("批量删除菜品信息 {}", ids);
        dishService.delBatch(ids);
        return Result.success();
    }

}
